<?php
	defined( '_JEXEC' ) or die( 'Restricted access' );
	jimport('joomla.application.component.view');
	class SocialRoutesViewRoute extends JView
	{
		var $_option = null;
		
		function display($tpl = null)
		{
			$option=JRequest::getCmd('option');

			$modelRoute = &$this->getModel();
			
			$route= $modelRoute->getRoute();
			$myRoutesTask = $modelRoute->getMyRoutesTask($route);
			$situation = 404040;
			
			$comments=$this->parseComments();
			
			$this->assignRef('option', $option);
			$this->assignRef('route', $route);
			$this->assignRef('myRoutesTask', $myRoutesTask);
			$this->assignRef('comments', $comments);
			$this->assignRef('situation', $situation);
			
			$this->addCSS();
			
			$this->addTraducCommonJavascript();
			$this->addTraducViewJavascript();
			$this->addJavascripts();
			$this->addRouteJavascript();
			$this->addPOIJavascript();
			
			parent::display($tpl);
		}
		
		private function addCSS(){
			$option=JRequest::getCmd('option');
			
			$document = &JFactory::getDocument();
			$document->addStyleSheet('components/'.$option.'/css/socialroutes.css');
		}
		
		private function addJavascripts(){
			$option=JRequest::getCmd('option');
			$jsPath='components/'.$option.'/js/';
			
			$document = &JFactory::getDocument();
			$document->addScript( 'http://maps.google.com/maps/api/js?sensor=false' );
			$document->addScript( 'https://www.google.com/jsapi' );
			$document->addScript( 'libraries/tiny_mce/tiny_mce.js' );
			$document->addScript( 'components/'.$option.'/js/jquery-1.5.1.min.js' );
			$js= 'tinyMCE.init({
					mode : "none",
					theme: "advanced",
					theme_advanced_buttons1 : "bold,italic,underline,separator,styleselect,formatselect",
					theme_advanced_buttons2 : "bullist,numlist,separator,outdent,indent,separator,undo,redo,separator,link,image",
					theme_advanced_buttons3 :  "",
					theme_advanced_toolbar_location : "top"
				});';
			$document->addScriptDeclaration($js);
			$document->addScript( $jsPath.'routeCommons.js' );
			$document->addScript( $jsPath.'routeVisualization.js' );
			$document->addScript( $jsPath.'jqSearch.js' );
		}
		
		private function addPOIJavascript(){
			$model = &$this->getModel();
			$pois= $model->getRoute()->getPois();
			$modelPOIs=&$this->getModel('SocialRoutesPOI');
			$poiCategories=$modelPOIs->getPOICategories();
			?>
			<script>
				var pois = new Array();
				var poiCategories = new Array();
				<?php
				for ($i = 0, $total = count($pois); $i < $total; $i ++){
					echo "\n var poi=new PointOfInterest('".$pois[$i]->getLocation()->getLat()."','".$pois[$i]->getLocation()->getLng()."','OLD');";
					echo "\n poi.setName('".$pois[$i]->getName()."');";
					echo "\n poi.setDescrip('".$pois[$i]->getDescription()."');";
					echo "\n poi.setId('".$pois[$i]->getId()."');";
					echo "\n poi.setImage('".$pois[$i]->getSubCategory()->getImage()."');";
					echo "\n pois[".$i."] = poi;"; 
				}
				for ($i = 0, $total = count($poiCategories); $i < $total; $i ++){
					echo "\n var poiCategory=new POICategory('".$poiCategories[$i]->getId()."');";
					echo "\n poiCategory.setName('".$poiCategories[$i]->getName()."');";
					echo "\n poiCategory.setDescription('".$poiCategories[$i]->getDescription()."');";
					$poiSubCategories=$poiCategories[$i]->getSubCategories();
					echo "\n var poiSubCategories = new Array();";
					for($j =0, $totalSub = count($poiSubCategories);$j<$totalSub; $j++){
						echo "\n var poiSubCategory=new POISubCategory('".$poiSubCategories[$j]->getId()."');";
						echo "\n poiSubCategory.setName('".$poiSubCategories[$j]->getName()."');";
						echo "\n poiSubCategory.setImage('".$poiSubCategories[$j]->getImage()."');";
						echo "\n poiSubCategory.setCategory(poiCategory);";
						echo "\n poiSubCategory.setDescription('".$poiSubCategories[$j]->getDescription()."');";
						echo "\n poiSubCategories[".$j."] = poiSubCategory;";
					}
					echo "\n poiCategory.setSubCategories(poiSubCategories);";
					echo "\n poiCategories[".$i."] = poiCategory;";
				}
				?>
			</script>
			<?php 
		}
		
		private function addRouteJavascript(){
			$model = &$this->getModel();
			$route= $model->getRoute();
			$coordinates=$route->getCoordinates();
			$pois=$route->getPois();
			?>
			<script>
				var isCreator=false;
				var routeID=0;
				var route = new Array();
				<?php
					echo "\n routeID='".$route->getId()."';";
					echo "\n isCreator='".$model->getIsCreator($route)."';";
					for ($i = 0, $total = count($coordinates); $i < $total; $i ++){
						echo "\n var latLng=new Array();";
						echo "\n latLng[0]= '".$coordinates[$i]->getLat()."';";
						echo "\n latLng[1]= '".$coordinates[$i]->getLng()."';";
						echo "\n route[".$i."] = latLng;"; 
					}
				?>
			</script>
			<?php 
		}
		
		private function addTraducCommonJavascript(){
			?>
			<script>
				<?php 
				echo "\n var R_COMMON_ELEVATION='".JText::_('R_COMMON_ELEVATION')."';";
				echo "\n var R_COMMON_MAXELEV='".JText::_('R_COMMON_MAXELEV')."';";
				echo "\n var R_COMMON_MINELEV='".JText::_('R_COMMON_MINELEV')."';";
				echo "\n var R_COMMON_DISTANCE='".JText::_('R_COMMON_DISTANCE')."';";
				echo "\n var R_COMMON_TIME='".JText::_('R_COMMON_TIME')."';";
				echo "\n var R_COMMON_DAY='".JText::_('R_COMMON_DAY')."';";
				echo "\n var R_COMMON_DAYS='".JText::_('R_COMMON_DAYS')."';";
				echo "\n var R_COMMON_MINUTES='".JText::_('R_COMMON_MINUTES')."';";
				echo "\n var R_COMMON_HOURS='".JText::_('R_COMMON_HOURS')."';";
				echo "\n var R_COMMON_HOUR='".JText::_('R_COMMON_HOUR')."';";
				echo "\n var R_COMMON_AND='".JText::_('R_COMMON_AND')."';";
				?>
			</script>
			<?php 
		}
		
		private function addTraducViewJavascript(){
			?>
			<script>
				<?php 
				echo "\n var R_VIEW_CONTROLTITLE='".JText::_('R_VIEW_CONTROLTITLE')."';";
				echo "\n var R_VIEW_NAME='".JText::_('R_VIEW_NAME')."';";
				echo "\n var R_VIEW_SUBMIT='".JText::_('R_VIEW_SUBMIT')."';";
				echo "\n var R_VIEW_CATEGORY='".JText::_('R_VIEW_CATEGORY')."';";
				echo "\n var R_VIEW_DESCRIPTION='".JText::_('R_VIEW_DESCRIPTION')."';";
				echo "\n var R_VIEW_DELETE='".JText::_('R_VIEW_DELETE')."';";
				echo "\n var R_VIEW_ALERTNAMEDESCRIP='".JText::_('R_VIEW_ALERTNAMEDESCRIP')."';";
				?>
			</script>
			<?php 
		}
		
		private function parseComments(){
			$model=&$this->getModel();
			$comments=$model->getRouteComments();
			for($i = 0; $i < count($comments); $i++)
			{
				$row =& $comments[$i];
				$row->setDate(JHTML::Date($row->getDate(),"%A, %d %B %Y %H:%M:%S"));
			}
			return $comments;
		}
	}
?>